home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / util / cli / LibMon.lha / Src / libinfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-28  |  2.7 KB  |  105 lines

  1. #include <exec/types.h>
  2. #include <exec/nodes.h>
  3. #include <exec/lists.h>
  4. #include <exec/libraries.h>
  5. #include <exec/execbase.h>
  6. #include <exec/memory.h>
  7. #include <clib/alib_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <string.h>
  11.  
  12. #include "libinfo.h"
  13.  
  14. extern struct ExecBase *SysBase;
  15.  
  16.  
  17. /*
  18. ** Build the Library Info List from all libraries in memory.
  19. */
  20. struct List *BuildLibInfoList(void)
  21. {
  22.   struct List *LibInfoList;
  23.   struct Library *Library;
  24.   ULONG NoErr;
  25.   
  26.   /* Get some memory for the List structure */
  27.   LibInfoList = (struct List *)AllocVec(sizeof(struct List),MEMF_CLEAR);
  28.   if (!LibInfoList)
  29.     /* Uh Oh... */
  30.     return NULL;
  31.   NewList(LibInfoList);
  32.   
  33.   Forbid();   /* Keep the system to our selves */
  34.   
  35.   Library = (struct Library *)SysBase->LibList.lh_Head;
  36.   NoErr = TRUE;
  37.   while (Library->lib_Node.ln_Succ && NoErr)
  38.   {
  39.     /* Try to add the current library to the list */
  40.     NoErr = (ULONG)AddLibInfoItem(LibInfoList, Library);
  41.     Library = (struct Library *)Library->lib_Node.ln_Succ;
  42.   }
  43.   
  44.   Permit();  /* Free the system for other to use (How kind of us!) */
  45.   
  46.   if (!NoErr)
  47.   {
  48.     /* The last call to AddLibInfoItem returned NULL (memory error) */
  49.     /* So we'll kill the LibInfo list and return NULL               */
  50.     KillLibInfoList(LibInfoList);
  51.     LibInfoList = NULL;
  52.   }
  53.   
  54.   return LibInfoList;
  55. }
  56.  
  57.  
  58. /*
  59. ** Add specified library to the Library Info List.
  60. ** Returns pointer to new item or NULL if something went wrong...
  61. */
  62. struct LibInfo *AddLibInfoItem(struct List *LibInfoList,
  63.                                struct Library *Library)
  64. {
  65.   struct LibInfo *LibInfoItem;
  66.   
  67.   /* Allocate memory for new item */
  68.   LibInfoItem = (struct LibInfo *)AllocVec(sizeof(struct LibInfo),MEMF_CLEAR);
  69.   if (!LibInfoItem)
  70.     /* Oops!  Ran out of memory! */
  71.     return NULL;
  72.   
  73.   /* OK, got the memory, now setup our fields... */
  74.   strcpy(LibInfoItem->Name,Library->lib_Node.ln_Name);
  75.   LibInfoItem->Address = (APTR)Library;
  76.   LibInfoItem->Version = Library->lib_Version;
  77.   LibInfoItem->Revision = Library->lib_Revision;
  78.   LibInfoItem->Priority = Library->lib_Node.ln_Pri;
  79.   LibInfoItem->OpenCount = Library->lib_OpenCnt;
  80.   LibInfoItem->Flushed = FALSE;
  81.   
  82.   /* Now add the new item to the list... */
  83.   if (LibInfoList)
  84.     /* This condition is here so we can create a single item without */
  85.     /* a list (see list.c/ListLib())                                 */
  86.     AddTail(LibInfoList,(struct Node *)LibInfoItem);
  87.   
  88.   /* Return the new item */
  89.   return LibInfoItem;
  90. }
  91.  
  92.  
  93. /*
  94. ** Destroy the entire Library Info List.
  95. */
  96. void KillLibInfoList(struct List *LibInfoList)
  97. {
  98.   struct LibInfo *LibInfoItem;
  99.   
  100.   while (( LibInfoItem = (struct LibInfo *)RemTail(LibInfoList) ))
  101.     FreeVec((APTR)LibInfoItem);
  102.   
  103.   FreeVec((APTR)LibInfoList);
  104. }
  105.